﻿/** \file
* \$Rev: 189 $
* 
* \$Date: 2010-06-28 03:02:44 +0000 (Mon, 28 Jun 2010) $
*
* \$URL: http://chorus-twitter.googlecode.com/svn/trunk/src/twitter/Status.cs $
*/

using System;
using System.Collections.Generic;
using System.Drawing;
using chorus.utils;

namespace chorus.twitter
{
	/// <summary>
	/// A Twitter status object.
	/// </summary>
	public class status : Message
	{
		#region XML serialization

		/// <summary>Usually describes the originating Twitter client.</summary>
		public string source { get; set; }

		/// <summary>Whether the status text is truncated.</summary>
		public SerStruct<bool> truncated { get; set; }

		/// <summary>The ID of the status to which this tweet is replying.</summary>
		public SerStruct<ulong> in_reply_to_status_id { get; set; }

		/// <summary>The ID of the user to which this tweet is replying.</summary>
		public SerStruct<ulong> in_reply_to_user_id { get; set; }

		/// <summary>Whether this status has been favourited by the authenticating user.</summary>
		public SerAccountDict<bool> favorited
		{
			get { return null; }
			set { value.MergeDict(favoriteddict); }
		}

		/// <summary>The name of the user to which this tweet is replying.</summary>
		public string in_reply_to_screen_name { get; set; }

		/// <summary>The user to which this tweet is directed.</summary>
		public user to;

		/// <summary>The user that wrote the status (internal).</summary>
		public user user { get; set; }

		#endregion

		/// <summary>The map of account IDs to "favorited" flags.</summary>
		public readonly Dictionary<UInt64, bool> favoriteddict = new Dictionary<UInt64, bool>();

		#region Properties

		/// <summary>The user that wrote this status.</summary>
		public override user From { get { return user; } }

		/// <summary>The user to which this tweet is directed.</summary>
		public override user To
		{
			get
			{
				if (to != null)
					return to;
				if (About != null)
				{
					to = About.From;
					return to;
				}
				if (in_reply_to_user_id != null)
					if (Program.users.byid.TryGetValue(in_reply_to_user_id.Coalesce, out to))
						return to;
				if (in_reply_to_screen_name != null && in_reply_to_screen_name.Length != 0)
					if (Program.users.byname.TryGetValue(in_reply_to_screen_name, out to))
						return to;
				return null;
			}
		}

		/// <summary>The user that this tweet is about.</summary>
		public status About
		{
			get
			{
				if (in_reply_to_status_id != null)
				{
					status aboutstatus;
					if (Program.statuses.byid.TryGetValue(in_reply_to_status_id.Coalesce, out aboutstatus))
						return aboutstatus;
				}
				return null;
			}
		}

		/// <summary>The status line describing message details.</summary>
		public override string StatLine
		{
			get
			{
				string stat = base.StatLine;
				if (To != null)
					stat += " replying to @" + To.screen_name;
				if (About != null)
					stat += "'s tweet";
				stat += Environment.NewLine + "via " + source;
				return stat;
			}
		}

		#endregion
	}

	/// <summary>
	/// A response object for a collection of statuses.
	/// </summary>
	public class statuses : IMessageCollectionResponse
	{
		/// <summary>The statuses in the response.</summary>
		public status[] status;

		/// <summary>The response message collection.</summary>
		public IEnumerable<Message> Messages { get { return status; } }
	}
}
